﻿Imports System.Collections.Generic
Imports InvoiceSample
Imports CodeProject.Data.Entity
Imports System.Data.Objects

Public Class BaseInvoiceTestsVB

#Region "Helper Methods"

	Private _contactsCache As Dictionary(Of Long, Contact)

	Protected ReadOnly Property Contacts() As Dictionary(Of Long, Contact)
		Get

			If (_contactsCache Is Nothing) Then
				Dim result As New Dictionary(Of Long, Contact)
				Using context = New SampleInvoiceContext()
					context.ContactSet.MergeOption = MergeOption.NoTracking
					For Each item In context.ContactSet
						result(item.ID) = item
					Next
				End Using
				_contactsCache = result
			End If

			Return _contactsCache

		End Get
	End Property

	Protected ReadOnly Property Suppliers() As Dictionary(Of Long, Supplier)
		Get
			Dim result As New Dictionary(Of Long, Supplier)

			For Each item As Contact In Me.Contacts.Values
				If (TypeOf item Is Supplier) Then
					result(item.ID) = CType(item, Supplier)
				End If
			Next

			Return result

		End Get
	End Property

	Protected Function GetDetachedProductWithSupplier(ByVal ID As Long) As Product

		Using context = New SampleInvoiceContext()

			Dim prodq = From prod _
			In context.ProductSet.Include(Function(p) p.Supplier) _
			 Where prod.ID = ID _
			 Select prod

			Return context.DetachObjectGraph(prodq.First())

		End Using

	End Function

#End Region

End Class
